2023.11.16 交差点と事故座標とのマッチング
多重ループを排除し、処理の高速化を図ることが目的。
まずは小規模な問題でテスト。
code:inter_accident.py
import numpy as np
x_min, x_max = 0, 9
y_min, y_max = 0, 9
# 交差点とその座標
inter_num_square = 5 # 交差点数の平方根
inter_x = np.linspace(x_min, x_max, inter_num_square)
inter_y = np.linspace(y_min, y_max, inter_num_square)
inter_x, inter_y = np.meshgrid(inter_x, inter_y)
inter_x, inter_y = inter_x.reshape(-1), inter_y.reshape(-1)
class Intersection:
def __init__(self, x, y):
self.x = x
self.y = y
self.accident = []
intersections = []
for tmp_x, tmp_y in zip(inter_x, inter_y):
intersections.append(Intersection(tmp_x, tmp_y))
# 事故座標
# * 交差点の付近に生成する
accident_num = 5
accident_x, accident_y = [], []
print('乱数で生成した事故発生交差点のID')
for i in range(accident_num):
select = np.random.randint(len(intersections))
print(select, end=' ' )
accident_x.append(inter_xselect + (np.random.rand()-0.5)/5) accident_y.append(inter_yselect + (np.random.rand()-0.5)/5) accident_x = np.array(accident_x)
accident_y = np.array(accident_y)
print()
# 判定
# * ブールインデックス参照
threshold = 0.1
print('座標でマッチした事故発生交差点のID')
for i in range(len(accident_x)):
x_check = np.abs(inter_x - accident_xi) < threshold y_check = np.abs(inter_y - accident_yi) < threshold xy_check = x_check*y_check # 論理値の積でand処理
match = np.where(xy_check == True)00 # タプル中のndarray中の値を取り出している print(match, end=' ')
intersectionsmatch.accident.append('事故!') # 結果の確認
for num, inter in enumerate(intersections):
print(num, inter.accident)
うまく動作しているようだ。
マップを広げ、事故件数を増やしてみる。
x_min, x_max = 0, 100
y_min, y_max = 0, 100
accident_num = 5 -> 10000
code:inter_accident_match2.py
import numpy as np
import time
from tqdm import tqdm
x_min, x_max = 0, 100
y_min, y_max = 0, 100
# 交差点とその座標
inter_num_square = 1000 # 交差点数の平方根
inter_x = np.linspace(x_min, x_max, inter_num_square)
inter_y = np.linspace(y_min, y_max, inter_num_square)
inter_x, inter_y = np.meshgrid(inter_x, inter_y)
inter_x, inter_y = inter_x.reshape(-1), inter_y.reshape(-1)
class Intersection:
def __init__(self, x, y):
self.x = x
self.y = y
self.accident = []
intersections = []
for tmp_x, tmp_y in zip(inter_x, inter_y):
intersections.append(Intersection(tmp_x, tmp_y))
# 事故座標
# * 交差点の付近に生成する
accident_num = 10000
accident_x, accident_y = [], []
print('乱数で生成した事故発生交差点のID')
for i in tqdm(range(accident_num)):
select = np.random.randint(len(intersections))
# print(select, end=' ' )
accident_x.append(inter_xselect + (np.random.rand()-0.5)/5) accident_y.append(inter_yselect + (np.random.rand()-0.5)/5) accident_x = np.array(accident_x)
accident_y = np.array(accident_y)
print()
# 判定
# * キーワード:ブールインデックス参照
t1 = time.time() # ここから時間計測
threshold = 0.1 # 交差点と事故座標間の距離の閾値
print('座標でマッチした事故発生交差点のID')
for i in tqdm(range(len(accident_x))):
x_check = np.abs(inter_x - accident_xi) < threshold y_check = np.abs(inter_y - accident_yi) < threshold xy_check = x_check*y_check # 論理値の積でand処理
match = np.where(xy_check == True)00 # タプル中のndarray中の値を取り出している # print(match, end=' ')
intersectionsmatch.accident.append('事故!') t2 = time.time()
# 結果の確認
for num, inter in enumerate(intersections):
if inter.accident != []:
print(num, inter.accident)
print('交差点の数', inter_num_square**2)
print('事故件数', accident_num)
print('マッチングに要した時間', t2 - t1)
結果:
code:result.txt
交差点の数 1000000
事故件数 10000
マッチングに要した時間 48.2010545730590
更に、事故件数を増やした結果(1万件 -> 10万件):
code:result.txt
交差点の数 1000000
事故件数 100000
マッチングに要した時間 446.525943756103
この場合、事故件数を10倍にすると、時間も約10倍になった。